package com.homwee.serialportinspection.tv;

import android.bluetooth.BluetoothDevice;
import android.os.Message;
import android.util.Log;

import com.homwee.serialportinspection.model.RequestHandler;
import com.homwee.serialportinspection.model.Types;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TvRequstHandler implements RequestHandler {
    private static final String TAG = "TvRequstHandler";
    private Tv mTv;

    public TvRequstHandler(Tv tv) {
        mTv = tv;
    }

    @Override
    public Message handle(Message request) {
        if (request.what < 0 || request.what >= Types.Request.values().length) {
            Log.e(TAG, "invalid what = " + request.what);
            return null;
        }

        //Types.Request reqType = Types.Request.values()[request.what];
        Types.Request_CH reqType = Types.Request_CH.values()[request.what];
        Message response = Message.obtain();
        response.what = request.what;   // save the request type in response for Controller to known what the response is for.

        switch (reqType) {
			/*
            case POWER_OFF:
                powerOff(request, response);
                break;

            case STANDBY:
                standby(request, response);
                break;

            case SET_SOURCE:
                setInputSource(request, response);
                break;

            case GET_SOURCE:
                getInputSource(request, response);
                break;

            case SET_CHANNEL:
                setChannel(request, response);
                break;

            case GET_CHANNEL:
                getChannel(request, response);
                break;

            case SET_VOLUME:
                setVolume(request, response);
                break;

            case GET_VOLUME:
                getVolume(request, response);
                break;

            case SET_SPDIF_MODE:
                setSpdifMode(request, response);
                break;

            case GET_SPDIF_MODE:
                getSpdifMode(request, response);
                break;

            case SET_PICTURE_MODE:
                setPictureMode(request, response);
                break;

            case GET_PICTURE_MODE:
                getPictureMode(request, response);
                break;

            case ENTER_FACTORY_MODE:
                enterFactoryMode(request, response);
                break;

            case EXIT_FACTORY_MODE:
                exitFactoryMode(request, response);
                break;

            case ENTER_FACTORY_MENU:
                enterFactoryMenu(request, response);
                break;

            case EXIT_FACTORY_MENU:
                exitFactoryMenu(request, response);
                break;

            case FACTORY_INIT:
                factoryReset(request, response);
                break;

            case SET_BACKLIGHT:
                setBacklight(request, response);
                break;

            case GET_BACKLIGHT:
                getBacklight(request, response);
                break;

            case GET_SW_VERSION:
                getSwVersion(request, response);
                break;

            case RESET_WB:
                resetWB(request, response);
                break;

            case SET_WB_PART:
                setWbPart(request, response);
                break;

            case GET_WB_PART:
                getWbPart(request, response);
                break;

            case SET_CT_MODE:
                setColorTempMode(request, response);
                break;

            case GET_CT_MODE:
                getColorTempMode(request, response);
                break;

            case SHOW_WB_BEFORE:
                showColorTempValue(request, response);
                break;

            case SHOW_WB_AFTER:
                hideColorTempValue(request, response);
                break;

            case SHOW_PATTERN:
                showPattern(request, response);
                break;

            case CLOSE_PATTERN:
                closePattern(request, response);
                break;

            case FACTORY_SET_WB:
                factorySetWB(request, response);
                break;

            case FACTORY_GET_WB:
                factoryGetWB(request, response);
                break;

            case FACTORY_WB_PATTERN:
                factoryWbPattern(request, response);
                break;

            case SET_SOUND_MODE:
                setSoundMode(request, response);
                break;

            case GET_SOUND_MODE:
                getSoundMode(request, response);
                break;

            case SET_MUTE:
                setMute(request, response);
                break;

            case GET_MUTE:
                getMute(request, response);
                break;

            case SET_SOUND_BALANCE:
                setSoundBalance(request, response);
                break;
            case GET_SOUND_BALANCE:
                getSoundBalance(request, response);
                break;

            case GET_USB_STORAGE_CNT:
                getUsbStorageCnt(request, response);
                break;

            case SET_ETH_MAC:
                setEthMac(request, response);
                break;

            case GET_ETH_MAC:
                getEthMac(request, response);
                break;

            case GET_ETH_IP:
                getEthIp(request, response);
                break;

            case GET_WIFI_MAC:
                getWifiMac(request, response);
                break;

            case ENABLE_WIFI:
                enableWifi(request, response);
                break;

            case DISABLE_WIFI:
                disableWifi(request, response);
                break;

            case START_WIFI_SCAN:
                startWifiScan(request, response);
                break;

            case GET_WIFI_SCANRESULT:
                getWifiScanResult(request, response);
                break;

            case GET_BT_VERSION:
                getBtVersion(request, response);
                break;

            case GET_BT_MAC:
                getBtMac(request, response);
                break;

            case START_BT_DISCOVERY:
                startBtDiscovery(request, response);
                break;

            case GET_BT_DISCOVERIED_DEV:
                getBtDiscoveryDev(request, response);
                break;

            case GET_BT_CONNECTED_DEV:
                getBtConnectedDev(request, response);
                break;

            case GET_SERIAL_NUMBER:
                getSerialNumber(request, response);
                break;

            case GET_PROJECT_ID:
                getProjectID(request, response);
                break;

            case GET_AQ_VERSION:
                getAQVersion(request, response);
                break;

            case GET_PQ_VERSION:
                getPQVersion(request, response);
                break;

            case GET_PANEL_VERSION:
                getPanelVersion(request, response);
                break;

            case GET_DATA_VERSION:
                getDataVersion(request, response);
                break;

            case GET_COMPILE_TIME:
                getBuildTime(request, response);
                break;

            case GET_PANEL_NAME:
                getPanelName(request, response);
                break;

            case GET_HDCP_1_4:
                getHdcpKey14(request, response);
                break;

            case GET_HDCP_2_2:
                getHdcpKey2(request, response);
                break;

            case GET_WIDEVINE_STATUS:
                getWidevineKey(request, response);
                break;

            case GET_KEYMASTER_STATUS:
                getKeymaster(request, response);
                break;

            case GET_PLAYREADY_STATUS:
                getPlayready(request, response);
                break;

            case GET_PLAYREADY_2_5_STATUS:
                getPlayReady25(request, response);
                break;

            case SEND_KEY:
                sendKey(request, response);
                break;
         */
			case WB_CONNECT:
                setWBConnet(request, response);
				break;
			case WB_PREPARE:
				wBPrepare(request,response);
				break;
			case WB_COLORTEMP_OPTION:
                wBColorTempOpt(request, response);
				break;
			case WB_PICTUR_MODE_OPTION:
				wBPictureModeOpt(request,response);
				break;
			case WB_OSD_BACKLIGHT_MAX:
				WBBacklightMax(request, response);
				break;
			case WB_OSD_BACKLIGHT_MIN:
				WBBacklightMin(request, response);
				break;
			case WB_R_GAIN_SET:
			case WB_G_GAIN_SET:
			case WB_B_GAIN_SET:
				WBGainSet(request, response);
				break;
			case WB_STAND_SAVE:
			case WB_WARM_SAVE:
			case WB_COOL_SAVE:
				WBSave(request,response);
				break;
			case WB_R_GAIN_GET:
			case WB_G_GAIN_GET:
			case WB_B_GAIN_GET:
				WBGainGet(request, response);
				break;
			case CONNECT:
                setConnet(request, response);
				break;
			case ARC_CEC_OPT:
				CECOpt(request, response);
				break;
			case KPORT_OFF:
				KPortOff(request, response);
				break;
			case GET_KEYCODE:
				getKeyCode(request, response);
				break;
			case KPORT_ON:
				KPortOn(request, response);
				break;
			case GET_LAN_IP:
                getEthIp_CH(request, response);
				break;
 			case WIFI_SCAN:
               startWifiScan(request, response); 
               break;
			case PRESET_CHANNEL:
			   presetChannel(request, response);
			   break;
			case BT_SCAN:
			   startBtDiscovery_CH(request,response);
			   break;
			case GET_VER_INFO:
			   getVerInfo(request, response);
			   break;
			case KEY_STATUS_CHECK:
			   getAllKeyStatus(request, response);
			   break;


            default:
                response.arg1 = Types.RESPONSE_ERR;
                break;
        }

        request.recycle();
        return response;
    }

    /************** function. ****************/
    private void powerOff(Message request, Message response) {
        boolean result = mTv.powerOff();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void standby(Message request, Message response) {
        boolean result = mTv.standby();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void setInputSource(Message request, Message response) {
        if (request.arg1 < 0 || request.arg1 >= Types.InputSource.values().length) {
            response.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.InputSource inputSource = Types.InputSource.values()[request.arg1];
            boolean result = mTv.setInputSource(inputSource);
            response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
        }
    }

    private void getInputSource(Message request, Message response) {
        int source = mTv.getInputSource();
        response.arg1 = source;
    }

    private void setChannel(Message request, Message response) {
        if (request.arg1 < 0 || request.arg1 >= Types.ChannelType.values().length) {
            Log.e(TAG, "invalid channel type = " + request.arg1);
            response.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.ChannelType type = Types.ChannelType.values()[request.arg1];
            boolean result = mTv.playChannel(type, request.arg2);
            response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
        }
    }

    private void getChannel(Message request, Message response) {
        int reqChnType = request.arg1;
        int[] values = new int[2];
        mTv.getCurrChannel(values);
        response.arg1 = ((values[0] == reqChnType) || (values[0] != Types.ChannelType.ATV.ordinal() && reqChnType != Types.ChannelType.ATV.ordinal())) ? values[0] : Types.RESPONSE_ERR;
        response.arg2 = values[1];
    }

    private void setVolume(Message request, Message response) {
        boolean result = mTv.setVolume(request.arg1);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void getVolume(Message request, Message response) {
        int volume = mTv.getVolume();
        response.arg1 = volume;
    }

    private void setSpdifMode(Message request, Message response) {
        if (request.arg1 < 0 || request.arg1 >= Types.SpdifMode.values().length) {
            Log.e(TAG, "invalid spdif mode = " + request.arg1);
            response.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.SpdifMode mode = Types.SpdifMode.values()[request.arg1];
            boolean result = mTv.setSpdifMode(mode);
            response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
        }
    }

    private void getSpdifMode(Message request, Message response) {
        int mode = mTv.getSpdifMode();
        response.arg1 = mode;
    }


    private void setPictureMode(Message request, Message response) {
        if (request.arg1 < 0 || request.arg1 >= Types.PictureMode.values().length) {
            Log.e(TAG, "invalid picture mode = " + request.arg1);
            response.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.PictureMode mode = Types.PictureMode.values()[request.arg1];
            boolean result = mTv.setPictureMode(mode);
            response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
        }
    }

    private void getPictureMode(Message request, Message response) {
        int mode = mTv.getPictureMode();
        response.arg1 = mode;
    }

    private void enterFactoryMode(Message request, Message response) {        
        boolean result = mTv.setMModeEnable(true);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void exitFactoryMode(Message request, Message response) {
        boolean result = mTv.setMModeEnable(false);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void enterFactoryMenu(Message request, Message response) {
        boolean result = mTv.enterFactoryMenu();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void exitFactoryMenu(Message request, Message response) {
        boolean result = mTv.exitFactoryMenu();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void factoryReset(Message request, Message response) {
        boolean result = mTv.factoryReset();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void setBacklight(Message request, Message response) {
        boolean result = mTv.setBacklight(request.arg1);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void getBacklight(Message request, Message response) {
        int backlight = mTv.getBacklight();
        response.arg1 = backlight;
    }

    private void getSwVersion(Message request, Message response) {
        String version = mTv.getSwVersion();
        response.arg1 = version.isEmpty() ? Types.RESPONSE_ERR : Types.RESPONSE_OK;
        response.obj = version;
    }

    private void resetWB(Message request, Message response) {
        boolean result = mTv.resetWB();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void setWbPart(Message request, Message response) {
        if (request.arg1 < 0 || request.arg1 >= Types.ColorPart.values().length) {
            Log.e(TAG, "invalid color part = " + request.arg1);
            response.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.ColorPart part = Types.ColorPart.values()[request.arg1];
            boolean result = mTv.setWB(part, request.arg2);
            response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
        }
    }


    private void getWbPart(Message request, Message response) {
        if (request.arg1 < 0 || request.arg1 >= Types.ColorPart.values().length) {
            Log.e(TAG, "invalid color part = " + request.arg1);
            response.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.ColorPart part = Types.ColorPart.values()[request.arg1];
            int value = mTv.getWB(part);
            response.arg1 = value;
        }
    }

    private void setColorTempMode(Message request, Message response) {
        if (request.arg1 < 0 || request.arg1 > Types.ColorTempMode.values().length) {
            Log.e(TAG, "Invalid color temperature = " + request.arg1);
            response.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.ColorTempMode mode = Types.ColorTempMode.values()[request.arg1];
            boolean result = mTv.setCtMode(mode);
            response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
        }
    }

    private void getColorTempMode(Message request, Message response) {
        int mode = mTv.getCtMode();
        response.arg1 = mode;
    }

    private void showColorTempValue(Message request, Message response) {
        boolean result = mTv.showCtValue();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void hideColorTempValue(Message request, Message response) {
        boolean result = mTv.hideCtValue();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void showPattern(Message request, Message response) {
        boolean result = mTv.showPattern(request.arg1);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void closePattern(Message request, Message response) {
        boolean result = mTv.closePattern();
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void factorySetWB(Message request, Message response) {
        Types.ColorTempMode ctMode = Types.ColorTempMode.values()[request.arg1];
        Types.ColorPart part = Types.ColorPart.values()[request.arg2];
        int gain = (Integer) request.obj;

        boolean result = mTv.factorySetWB(ctMode, part, gain);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void factoryGetWB(Message request, Message response) {
        Types.ColorTempMode ctMode = Types.ColorTempMode.values()[request.arg1];
        Types.ColorPart part = Types.ColorPart.values()[request.arg2];
        response.arg1 = mTv.factoryGetWB(ctMode, part);
    }

    private void factoryWbPattern(Message request, Message response) {
        boolean result = mTv.factoryWbPattern(request.arg1, request.arg2 != 0);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void setSoundMode(Message request, Message response) {
        if (request.arg1 < 0 || request.arg1 >= Types.SoundMode.values().length) {
            Log.e(TAG, "invalid sound mode = " + request.arg1);
            response.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.SoundMode mode = Types.SoundMode.values()[request.arg1];
            boolean result = mTv.setSoundMode(mode);
            response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
        }
    }

    private void getSoundMode(Message request, Message response) {
        int mode = mTv.getSoundMode();
        response.arg1 = mode;
    }

    private void setMute(Message request, Message response) {
        boolean result = mTv.setMute(request.arg1 != 0);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void getMute(Message request, Message response) {
        boolean isMute = mTv.isMute();
        response.arg1 = isMute ? 1 : 0;
    }

    private void setSoundBalance(Message request, Message reponse) {
        if (request.arg1 < 0 || request.arg1 > Types.Balance.values().length) {
            reponse.arg1 = Types.RESPONSE_ERR;
        } else {
            Types.Balance balance = Types.Balance.values()[request.arg1];
            Log.d(TAG, "balance" + balance);
            boolean success = mTv.setBalance(balance);
            reponse.arg1 = success ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
        }
    }

    private void getSoundBalance(Message request, Message reponse) {
        int mode = mTv.getBalance();
        reponse.arg1 = mode;
    }

    private void getUsbStorageCnt(Message request, Message response) {
        int cnt = mTv.getUsbStorageCnt();
        response.arg1 = cnt;
    }

    private void setEthMac(Message request, Message response) {
        boolean result = mTv.setEthMac((String) request.obj);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void getEthMac(Message request, Message response) {
        String mac = mTv.getEthMac();
        response.arg1 = mac.isEmpty() ? Types.RESPONSE_ERR : Types.RESPONSE_OK;
        response.obj = mac;
    }

    private void getEthIp(Message request, Message response) {
        String ip = mTv.getEthIp();
        response.arg1 = ip.isEmpty() ? Types.RESPONSE_ERR : Types.RESPONSE_OK;
        response.obj = ip;
    }

    private void getEthIp_CH(Message request, Message response) {
        String ip = mTv.getEthIp();
		Log.d(TAG,"getEthIp_CH "+ip);
        //response.arg1 = ip.isEmpty() ? Types.RESPONSE_ERR : Types.RESPONSE_OK;
		if(ip == null || ip.isEmpty()){
            ip = "0.0.0.0";
		}
        response.obj = ip;
    }

    private void getWifiMac(Message request, Message response) {
        String mac = mTv.getWifiMac();
        response.arg1 = mac.isEmpty() ? Types.RESPONSE_ERR : Types.RESPONSE_OK;
        response.obj = mac;
    }

    private void enableWifi(Message request, Message response) {
        boolean result = mTv.setWifiEnable(true);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void disableWifi(Message request, Message response) {
        boolean result = mTv.setWifiEnable(false);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void getWifiScanResult(Message request, Message response) {
        response.obj = mTv.getWifiScanResult();
    }

    private void startWifiScan(Message request, Message response) {
        int status = mTv.wifiScan();

        if (status == Types.WifiState.CONNECTED.ordinal()
                || status == Types.WifiState.DISCONNECTED.ordinal()) {
            response.arg1 = Types.RESPONSE_OK;
        } else if (status == Types.WifiState.CONNECTING.ordinal()
                || status == Types.WifiState.DISCONNECTING.ordinal()) {
            response.arg1 = -2; //busy
        } else {
            response.arg1 = -1; //Unknow or Suspend or error
        }
    }

    private void getBtVersion(Message request, Message response) {
        String version = mTv.getBtVersion();
        response.arg1 = version.isEmpty() ? Types.RESPONSE_ERR : Types.RESPONSE_OK;
        response.obj = version;
    }

    private void getBtMac(Message request, Message response) {
        String mac = mTv.getBtMac();
        response.arg1 = mac.isEmpty() ? Types.RESPONSE_ERR : Types.RESPONSE_OK;
        response.obj = mac;
    }

    private void startBtDiscovery(Message request, Message response) {
        response.arg1 = mTv.startBtDiscovery((String) request.obj) ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void startBtDiscovery_CH(Message request, Message response) {
        response.arg1 = mTv.startBtDiscovery((String) request.obj) ? 1 : 0;
    }

    private void getBtDiscoveryDev(Message request, Message response) {
        BluetoothDevice bt = mTv.getBtDiscoveryDev();
        response.arg1 = (bt == null) ? Types.RESPONSE_ERR : Types.RESPONSE_OK;
        response.obj = bt;
    }

    private void getBtConnectedDev(Message request, Message response) {
        String[] devs = mTv.getBtConnectedDev();
        response.arg1 = Types.RESPONSE_OK;
        response.obj = devs;
    }

    private void getSerialNumber(Message request, Message response) {
        String sn = mTv.getSerialNumber();
        response.arg1 = Types.RESPONSE_OK;
        response.obj = sn;
    }

    private void sendKey(Message request, Message response) {
        boolean result = mTv.sendKeyEvent(request.arg1);
        response.arg1 = result ? Types.RESPONSE_OK : Types.RESPONSE_ERR;
    }

    private void getProjectID(Message request, Message response) {
        String projectID = mTv.getProjectID();
        Log.d(TAG, "getProjectID: " + projectID);
        response.arg1 = Types.RESPONSE_OK;
        response.obj = projectID;
    }

    private void getAQVersion(Message request, Message response) {
        String aqVersion = mTv.getAQVersion();
        response.arg1 = Types.RESPONSE_OK;
        response.obj = aqVersion;
    }

    private void getPQVersion(Message request, Message response) {
        String pqVersion = mTv.getPQVersion();
        response.arg1 = Types.RESPONSE_OK;
        response.obj = pqVersion;
    }

    private void getPanelVersion(Message request, Message response) {
        String panelVersion = mTv.getPanelVersion();

        response.arg1 = Types.RESPONSE_OK;
        response.obj = panelVersion;
    }

    private void getDataVersion(Message request, Message response) {
        String dataVersion = mTv.getDataVersion();

        response.arg1 = Types.RESPONSE_OK;
        response.obj = dataVersion;
    }

    private void getBuildTime(Message request, Message response) {
        final String originalPatternUTC = "EEE MMM dd HH:mm:ss 'UTC' yyyy";
        final String originalPatternCST = "EEE MMM dd HH:mm:ss 'CST' yyyy";
        final String originalPatternGMT = "EEE MMM dd HH:mm:ss 'GMT' yyyy";
        final String newPattern = "dd/MM/yyyy-HH:mm:ss";
        String originalPattern = null;
        String buildTime = null;
        String timeString = mTv.getBuildTime();

        if (timeString.contains("UTC")) {
            originalPattern = originalPatternUTC;
        } else if (timeString.contains("CST")) {
            originalPattern = originalPatternCST;
        } else if (timeString.contains("GMT")) {
            originalPattern = originalPatternGMT;
        }

        buildTime = convertTime(timeString, originalPattern, newPattern);
        if (null != buildTime) {
            Log.d(TAG, "getBuildTime: " + buildTime);
            response.obj = buildTime;
            response.arg1 = Types.RESPONSE_OK;
        } else {
            Log.d(TAG, "buildTime is NULL ");
            response.obj = null;
            response.arg1 = Types.RESPONSE_ERR;
        }
    }

    private String convertTime(String timeString, String originalPattern, String newPattern) {

        String buildTime = null;

        if (null == timeString || null == originalPattern || null == newPattern) {
            Log.d(TAG, "The input string is invalid!");
            return null;
        }

        Log.d(TAG, "The original time : " + timeString);

        try {
            SimpleDateFormat isoFormat1 = new SimpleDateFormat(
                    originalPattern, Locale.ENGLISH);
            SimpleDateFormat isoFormat2 = new SimpleDateFormat(
                    newPattern, Locale.ENGLISH);
            Date date = isoFormat1.parse(timeString);
            if (null != date) {
                buildTime = isoFormat2.format(date);
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }

        return buildTime;
    }

    private void getPanelName(Message request, Message response) {
        String panelName = mTv.getPanelName();
        response.arg1 = Types.RESPONSE_OK;
        response.obj = panelName;
    }

    private void getHdcpKey14(Message request, Message response) {
        boolean ret = mTv.getHDCPKey10();

        response.arg1 = Types.RESPONSE_OK;
        response.obj = ret;
    }

    private void getHdcpKey2(Message request, Message response) {
        boolean ret = mTv.getHDCPKey2();

        response.arg1 = Types.RESPONSE_OK;
        response.obj = ret;
    }

    private void getWidevineKey(Message request, Message response) {
        boolean ret = mTv.getWideVineKey();

        response.arg1 = Types.RESPONSE_OK;
        response.obj = ret;
    }

    private void getKeymaster(Message request, Message response) {
        boolean ret = mTv.getKeymaster();

        response.arg1 = Types.RESPONSE_OK;
        response.obj = ret;
    }

    private void getPlayready(Message request, Message response) {
        boolean ret = mTv.getKeystatusPlayready();

        response.arg1 = Types.RESPONSE_OK;
        response.obj = ret;
    }

    private void getPlayReady25(Message request, Message response) {
        boolean ret = mTv.getKeystatusPlayready25();

        response.arg1 = Types.RESPONSE_OK;
        response.obj = ret;
    }

	private void setWBConnet(Message request, Message response){
        Log.e(TAG, "setWBConnet in ");
		boolean ret = mTv.setWBConnet();

		response.arg1 = Types.RESPONSE_OK;
		response.obj = ret;
	}

     private void  wBPrepare(Message request, Message response){
        Log.e(TAG, "wBPrepare in ");
		int source = request.arg1 + Types.InputSource.HDMI.ordinal() -1;
		int tempmode = request.arg2;
        Log.e(TAG, "wBPrepare source  "+source + " colcortemp "+tempmode);
            
		Types.InputSource inputSource = Types.InputSource.values()[source];
		mTv.setInputSource(inputSource);
		Types.ColorTempMode mode = Types.ColorTempMode.values()[tempmode -1];
        mTv.setCtMode(mode);
		response.what = request.what;
		response.arg1 = request.arg1;
		response.arg2 = request.arg2;

	 }

     private void  wBColorTempOpt(Message request, Message response){
        Log.e(TAG, "wBColorTempOpt in ");
		int opttype = request.arg1;
		//if(opttype == 0){
		//	response.arg1 = Types.RESPONSE_OK;
		//	response.obj = true;
		//	response.arg2 = mTv.getCtMode();
		//} else if(opttype > 0 && opttype < 5){
            
            Types.ColorTempMode mode = Types.ColorTempMode.values()[request.arg1 -1];
			response.arg1 = Types.RESPONSE_OK;
			response.arg2 = opttype;
			response.obj = mTv.setCtMode(mode);
		//}

	 }
	 private void wBPictureModeOpt(Message request,Message response){
        Log.e(TAG, "wBPictureModeOpt in ");
		int opttype = request.arg1;
		if(opttype == 0){
			response.arg1 = Types.RESPONSE_OK;
			response.obj = true;
			response.arg2 = mTv.getPictureMode();
		} else if(opttype > 0 && opttype < 11){
            
            Types.PictureMode mode = Types.PictureMode.values()[request.arg1];
			response.arg1 = Types.RESPONSE_OK;
			response.arg2 = opttype;
			response.obj = mTv.setPictureMode(mode);
		}

    }
	private void WBBacklightMax(Message request, Message response){
		int opttype = request.arg1;
        Log.e(TAG, "WBBacklightMax in opttype = "+opttype);
		if(opttype != -1){
			response.arg1 = Types.RESPONSE_ERR;
			response.obj = false;
		} else {
            
			response.arg1 = Types.RESPONSE_OK;
			response.arg2 = opttype;
			mTv.setBacklight(100);
			mTv.setContrast(100);//mTv.setPictureMode(mode);
			response.obj = true;
		}

	}

	private void WBBacklightMin(Message request, Message response){
		int opttype = request.arg1;
        Log.e(TAG, "WBBacklightMin in opttype = "+opttype);
		if(opttype != 0){
			response.arg1 = Types.RESPONSE_ERR;
			response.obj = false;
		} else {
            
			response.arg1 = Types.RESPONSE_OK;
			response.arg2 = opttype;
			mTv.setBacklight(0);
			mTv.setContrast(0);//mTv.setPictureMode(mode);
			response.obj = true;
		}

	}
	private void WBGainSet(Message request, Message response){
		int high = request.arg1;
		int low = request.arg2;
        Log.e(TAG, "WBGainSe in high = "+high+" low "+low);
            
		int gain = high << 8 | low;

        Types.Request_CH reqType = Types.Request_CH.values()[request.what];
        Log.e(TAG, "WBGainSe in rGain = "+gain);
		
        int ctMode = mTv.getCtMode();
        Types.ColorPart part = Types.ColorPart.RED;
		if(reqType == Types.Request_CH.WB_R_GAIN_SET){
			part = Types.ColorPart.RED;
		} else if(reqType == Types.Request_CH.WB_G_GAIN_SET){
			part = Types.ColorPart.GREEN;
		}else if(reqType == Types.Request_CH.WB_B_GAIN_SET){
			part = Types.ColorPart.BLUE;
		}
		
		response.arg1 = request.arg1;
		response.arg2 = request.arg2;
        response.obj = mTv.factorySetWB(Types.ColorTempMode.values()[ctMode], part, gain);
		/*
		if(gain < -100 || gain  > 100){
			response.arg1 = Types.RESPONSE_ERR;
			response.obj = false;

		} else {
			response.arg1 = request.arg1;
			response.arg2 = request.arg2;
			if(reqType == Types.Request_CH.WB_R_GAIN_SET){
			    response.obj = mTv.setWB(Types.ColorPart.RED, gain);
			} else if(reqType == Types.Request_CH.WB_G_GAIN_SET){
			    response.obj = mTv.setWB(Types.ColorPart.GREEN, gain);
			} else if(reqType == Types.Request_CH.WB_B_GAIN_SET){
			    response.obj = mTv.setWB(Types.ColorPart.BLUE, gain);
			} else {
			    response.obj = false;
			}
			//response.obj = true;
		}*/

	}
	private void WBSave(Message request,Message response){

		Types.Request_CH reqType = Types.Request_CH.values()[request.what];
        Log.d(TAG, "WBSave" + reqType);
		int Rhigh = request.getData().getInt("Rhigh");
		int Rlow = request.getData().getInt("Rlow");
		int Ghigh = request.getData().getInt("Ghigh");
		int Glow = request.getData().getInt("Glow");
		int Bhigh = request.getData().getInt("Bhigh");
		int Blow = request.getData().getInt("Blow");
		
		int RGain = Rhigh << 8 | Rlow;
		int GGain = Ghigh << 8 | Glow;
		int BGain = Bhigh << 8 | Blow;

        int ctMode = mTv.getCtMode();
		Types.FactoryColorTempData ctData = mTv.factoryGetWB();
		if(reqType == Types.Request_CH.WB_STAND_SAVE){
			ctMode = Types.ColorTempMode.STANDARD.ordinal();

		} else if(reqType == Types.Request_CH.WB_WARM_SAVE){
			ctMode = Types.ColorTempMode.WARM.ordinal();

		} else {
			ctMode = Types.ColorTempMode.COOL.ordinal();
		}
		ctData.rGain = RGain;
		ctData.gGain = GGain;
		ctData.bGain = BGain;
		mTv.factorySetWB(ctMode,ctData);
		response.setData(request.getData());
	}
	private void WBGainGet(Message request, Message response){

		int gain = 0;

		Types.Request_CH reqType = Types.Request_CH.values()[request.what];
		response.obj = true;
		Types.FactoryColorTempData ctData = mTv.factoryGetWB();

		if(reqType == Types.Request_CH.WB_R_GAIN_GET && ctData != null){
			gain = ctData.rGain;//mTv.getWB(Types.ColorPart.RED);
		} else if(reqType == Types.Request_CH.WB_G_GAIN_GET){
			gain = ctData.gGain;//mTv.getWB(Types.ColorPart.GREEN);
		} else if(reqType == Types.Request_CH.WB_B_GAIN_GET){
			gain = ctData.bGain;//mTv.getWB(Types.ColorPart.BLUE);
		} else {
			response.obj = false;
		}
		Log.e(TAG, "WBGainget in Gain = "+gain);
		if(gain < 0){
		    response.arg1 = 0xFF;//gain << 8 & 0xFF00;
		    response.arg2 = gain & 0x00FF;
		} else {
		    response.arg1 = (gain >> 8) & 0xFF;
		    response.arg2 = gain & 0xFF;

		}

		//response.obj = true;

	}


	private void setConnet(Message request, Message response){
        Log.e(TAG, "setConnet in ");
		boolean ret = mTv.setConnet();

		response.arg1 = Types.RESPONSE_OK;
		response.obj = ret;
	}

	private void CECOpt(Message request, Message response){
        Log.e(TAG, "CECOpt in ");
		boolean ret = true;
		if(request.arg1 == 0){
			response.arg2 = mTv.getCECStatus();
		} else {

		  ret =	mTv.setCECStatus(request.arg1) == 1;
		  response.arg2 = ret ? 1 : 0;
		}

		response.arg1 = request.arg1;//Types.RESPONSE_OK;
		response.obj = ret;
	}

	private void KPortOff(Message request, Message response){
        Log.e(TAG, "KPortOff in ");
		boolean ret = true;//mTv.setConnet();

		response.arg1 = request.arg1;
		response.obj = ret;
	}

	private void getKeyCode(Message request, Message response){
        Log.e(TAG, "getKeyCode in ");
		boolean ret = true;//mTv.setConnet();

		//response.arg1 = request.arg1;
		response.obj = ret;
	}

	private void KPortOn(Message request, Message response){
        Log.e(TAG, "KPortOn in ");
		boolean ret = true;//mTv.setConnet();

		response.arg1 = request.arg1;
		response.obj = ret;
	}

	private void presetChannel(Message request, Message response){
		int ret = mTv.presetChannel(request.arg1);
		response.arg1 = request.arg1;
		response.arg2 = ret;
	}

	private void getVerInfo(Message request, Message response){
		if(request.arg1 == 0){
			response.obj = mTv.getSwVersion();
		} else if(request.arg1 == 1){
			request.obj = mTv.getDataVersion();
		} else if(request.arg1 == 2){
			request.obj = mTv.getPQVersion();
		} else if(request.arg1 == 3){
			request.obj = mTv.getProjectID();
		} else if(request.arg1 == 4){
			request.obj = mTv.getBuildTime();
		}

		response.arg1 = request.arg1;
	}

	private void getAllKeyStatus(Message request, Message response){


	}
}
